import mcApi from 'mc-c2b2m-operator-api';
import { priceUnitList } from '@/enums/spuEnum';
import { BasicColumn, FormSchema } from '@/components/Table';
import { MapQuotationBizMetaAttrModel } from 'mc-c2b2m-operator-api/types/spu';

const { SpuModule } = mcApi;
const { BizMetaArrApi } = SpuModule;

export const tableColumns: BasicColumn[] = [
  { title: '名称', dataIndex: 'cnName' },
  { title: '描述', dataIndex: 'description' },
  {
    title: '计价方式',
    dataIndex: 'ladderDimType',
    customRender: ({ text }) => {
      return priceUnitList.find((find) => find.value === text)?.label;
    },
  },
  {
    title: '计价类型',
    dataIndex: 'ladderDimUnit',
    customRender: ({ text }) => {
      return priceUnitList.find((find) => find.value === text)?.label;
    },
  },
  {
    title: '业务元属性',
    dataIndex: 'bizMetaAttrList',
    customRender: ({ text }) => {
      return text?.map?.((item: MapQuotationBizMetaAttrModel) => item.cnName)?.join?.('、');
    },
  },
  { title: '阶梯维度计算倍数', dataIndex: 'ladderDimMultiple' },
  { title: '创建时间', dataIndex: 'createdTime' },
  { title: '更新时间', dataIndex: 'updatedTime' },
];
export const tableFormSchema: FormSchema[] = [
  {
    field: 'cnName',
    label: '名称',
    component: 'Input',
    colProps: { span: 6 },
  },
];

const bizMetaAttrSchema: FormSchema = {
  required: true,
  field: 'bizMetaAttrIdList',
  label: '业务元属性',
  component: 'ApiTreeSelect',
  componentProps: {
    showSearch: true,
    valueField: 'id',
    labelInValue: true,
    labelField: 'cnName',
    treeCheckStrictly: true,
    treeDefaultExpandAll: true,
    treeNodeFilterProp: 'cnName',
    childrenField: 'childBizMetaAttrList',
    api: BizMetaArrApi.getBizMetaAttrTree,
  },
};

export const modalFormSchema: FormSchema[] = [
  {
    field: 'id',
    label: 'id',
    ifShow: false,
    component: 'Input',
  },
  {
    ifShow: false,
    field: 'quotationType',
    label: 'quotationType',
    component: 'Input',
  },
  {
    required: true,
    field: 'cnName',
    label: '名称',
    component: 'Input',
  },
  {
    required: true,
    field: 'description',
    label: '描述',
    component: 'Input',
  },
  {
    required: true,
    field: 'ladderDimType',
    label: '计价方式',
    component: 'Select',
    componentProps: {
      options: priceUnitList,
    },
  },
  {
    required: true,
    field: 'ladderDimUnit',
    label: '计价类型',
    component: 'Select',
    componentProps: {
      options: priceUnitList,
    },
  },
  {
    ...bizMetaAttrSchema,
    field: 'bizMetaAttrIdList',
    label: '业务元属性',
  },
  {
    ...bizMetaAttrSchema,
    required: false,
    field: 'ladderDimBizMetaAttrIdList',
    label: '阶梯维度业务元属性',
  },
  {
    field: 'ladderDimMultiple',
    label: '阶梯维度计算倍数',
    component: 'InputNumber',
  },
];

export const detailTableColumns: BasicColumn[] = [
  { title: '维度最小值', dataIndex: 'dimMinValue' },
  { title: '维度最大值', dataIndex: 'dimMaxValue' },
  { title: '价格', dataIndex: 'price' },
  {
    title: '计价单位',
    dataIndex: 'priceUnit',
    customRender: ({ text }) => {
      return priceUnitList.find((find) => find.value === text)?.label;
    },
  },
  { title: '备注', dataIndex: 'comment' },
  { title: '创建时间', dataIndex: 'createdTime' },
  { title: '更新时间', dataIndex: 'updatedTime' },
];

export const detailFormSchema: FormSchema[] = [
  {
    ifShow: false,
    field: 'id',
    label: 'id',
    component: 'Input',
  },
  {
    ifShow: false,
    component: 'Input',
    field: 'ladderQuotationListId',
    label: 'ladderQuotationListId',
  },
  {
    required: true,
    field: 'dimMinValue',
    label: '维度最小值',
    component: 'InputNumber',
  },
  {
    required: true,
    field: 'dimMaxValue',
    label: '维度最大值',
    component: 'InputNumber',
  },
  {
    required: true,
    field: 'price',
    label: '价格',
    component: 'InputNumber',
  },
  {
    required: true,
    field: 'priceUnit',
    label: '计价单位',
    component: 'Select',
    componentProps: {
      options: priceUnitList,
    },
  },
  {
    field: 'comment',
    label: '备注',
    component: 'Input',
  },
];
